package com.fl.controller;

import java.io.IOException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.alibaba.fastjson.JSONObject;
import com.fl.util.BooleanHelper;
import com.fl.util.IntegerHelper;
import com.fl.util.PageParamInfo;

import com.google.common.primitives.Ints;

public class BaseController {


	
	protected PageParamInfo setPageInfo(Integer page,Integer limit){
		PageParamInfo p=new PageParamInfo();
		p.setPageInfo(page, limit);
		return p;
	}
	
	protected void Printer(HttpServletResponse response,String value) throws IOException{	
		response.getWriter().print(value);;
	}
	
	
	
	public int getIntParameter(HttpServletRequest request,String value)
	{
		String valueStr=this.getParameter(request, value);
		return IntegerHelper.getIntValue(valueStr);
	}

	public int getBooleanParameter(HttpServletRequest request,String value)
	{
		String valueStr=this.getParameter(request, value);
		return BooleanHelper.getIntValue(valueStr);
	}
	
	
	protected String getParameter(HttpServletRequest request,String value){	
		return request.getParameter(value) ==null ? "" : request.getParameter(value).toString();
	}
	
	protected String getFormParameter(HttpServletRequest request,String value){	
		return request.getParameter(value) ==null ? "" : request.getParameter(value).toString();
	}
	
	protected Integer getParameterIntDefault(HttpServletRequest request,String value,String defaultvalue){	
		return Integer.parseInt(getParameterDefault(request,value,defaultvalue));
	}
	
	protected String getParameterDefault(HttpServletRequest request,String value,String defaultvalue){	
		return request.getParameter(value) ==null ? defaultvalue : request.getParameter(value).toString();
	}
	
	protected int getPageNum(HttpServletRequest request){	
		return request.getParameter("pagenum") ==null ? 1 :Ints.tryParse(request.getParameter("pagenum").toString());
	}
	
	protected int getPageSize(HttpServletRequest request){	
		return request.getParameter("pagesize") ==null ? 20 : Ints.tryParse(request.getParameter("pagesize").toString());
	}

	
	public void  setUTF8(HttpServletResponse response)
	{
		response.setHeader("Content-type", "text/html;charset=UTF-8");  
		response.setContentType("application/json");
		
	}

	
	private  String   jsonstringkey="jsonstring";
	public JSONObject getJsonObject(HttpServletRequest request)
	{	
		String jsonstring=request.getParameter(jsonstringkey);
		JSONObject jsonobject = JSONObject.parseObject(jsonstring);
		return jsonobject;
	}

	
	
	
}
